#!/usr/bin/perl -w
use strict;
use lib 'lib';
use MT::Bootstrap;
use CGI::Fast;

my $handlers = {
    'mt.fcgi' => 'MT::App::CMS',
    'mt-comments.fcgi' => 'MT::App::Comments',
    'mt-tb.fcgi' => 'MT::App::Trackback',
    'mt-search.fcgi' => 'MT::App::Search',
## uncomment if necessary, but this adds a lot of
## overhead since it loads up LibXML.
##    'mt-atom.fcgi' => 'MT::AtomServer',
};
eval "use $_" foreach (values %$handlers);

eval {
    while (my $q = new CGI::Fast) {
	my($cgi) = $q->request_uri =~ m!.*/(.*\.fcgi)!;
	my $pkg = $handlers->{$cgi};
	die "Invalid handler for $cgi" unless $pkg;
	my $app = $pkg->new(CGIObject => $q) or die $pkg->errstr;
	local $SIG{__WARN__} = sub { $app->trace($_[0]) };
	MT->set_instance($app);
	$app->init_request(CGIObject => $q) unless $app->{init_request};
	$app->run;
	my $mode = $app->mode || '';
	if ("$pkg->$mode" eq 'MT::App::CMS->plugin_control') {
	    exit; # allows server to recycle after changing plugin switches
	}
    }
};
if ($@) {
    print "Content-Type: text/html\n\n";
    print "Got an error: $@";
}
